package util;

import java.io.Serializable;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email implements Serializable {
	private static final long serialVersionUID = -2686522381974340219L;
	
	private final String username = "sua.parte.contato@gmail.com"; 
	private final String password = "O02DPZh5";
	private final String smtp = "smtp.gmail.com"; 
	
	public static void main(String args[]){
		Email e = new Email();
		try {
			e.simpleEmail("valter.bcc@gmail.com", "teste", "teste");
		} catch (AddressException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (MessagingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	
	public void simpleEmail(String to, String subject, String message) throws AddressException, MessagingException {
		Properties props = System.getProperties();
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", smtp);
		props.put("mail.smtp.user", username);
		props.put("mail.smtp.password", password);
		props.put("mail.smtp.port", "587");
		props.put("mail.smtp.auth", "true");

		Session session = Session.getDefaultInstance(props, null);
		MimeMessage mmessage = new MimeMessage(session);
		try {

			InternetAddress toAddress = new InternetAddress(to);

			mmessage.setFrom(new InternetAddress(username));
			mmessage.addRecipient(Message.RecipientType.TO, toAddress);
			mmessage.setSubject(subject , "utf-8");
			
			String html = "<p>"+ message + "</p>" +
					      "<img src=\"http://i1179.photobucket.com/albums/x393/suaparte/website/logo_email.png\" /> ";
			mmessage.setContent(html, "text/html; charset=utf-8");
			
			// sending the message
			Transport transport = session.getTransport("smtp");
			transport.connect(smtp, username, password);
			transport.sendMessage(mmessage, mmessage.getAllRecipients());
			transport.close();

		} catch (AddressException ae) {
			throw new AddressException(ae.getMessage());

		} catch (MessagingException me) {
			throw new MessagingException(me.getMessage());
		}
	}
	
	private void simpleMultipleEmail(String[] to, String subject, String clientMessage) {
		String host = "smtp.sao.terra.com.br";

		String from = "valter@brainset.com.br";
		String pass = "u3t9i8";

		Properties props = System.getProperties();
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.user", from);
		props.put("mail.smtp.password", pass);
		props.put("mail.smtp.port", "587");
		props.put("mail.smtp.auth", "true");

		Session session = Session.getDefaultInstance(props, null);
		MimeMessage message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(from));

			InternetAddress[] toAddress = new InternetAddress[to.length];

			// To get the array of addresses
			for (int i = 0; i < to.length; i++) { // changed from a while loop
				toAddress[i] = new InternetAddress(to[i]);
			}

			System.out.println(Message.RecipientType.TO);

			for (int i = 0; i < toAddress.length; i++) { // changed from a while loop

				message.addRecipient(Message.RecipientType.TO, toAddress[i]);
				message.setSubject(subject);
				message.setText(clientMessage);
				
				Transport transport = session.getTransport("smtp");
				transport.connect(host, from, pass);
				transport.sendMessage(message, message.getAllRecipients());
				transport.close();
			}

		} catch (AddressException e) {
			e.printStackTrace();

		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}


}
